package gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import connection.Reader;

/**
 * Okno pro zmenu hesla
 * 
 * @author dove
 * 
 */
public class ChangePassWin extends InternalFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JButton ok;

	private JButton canc;

	private JPasswordField current;

	private JPasswordField newPass;

	private JPasswordField confirm;

	/**
	 * Nastavuje nazev okna
	 * 
	 * @param name
	 *            nazev okna
	 */
	public ChangePassWin(String name) {
		super(name);
	}

	public ChangePassWin getWindow() {

		JPanel pan = new JPanel();
		pan.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		c.insets = new Insets(10, 10, 10, 10);
		c.gridx = 0;
		c.gridy = 0;
		JLabel lcurrent = new JLabel(messages.getString("Current_pass") + ":");
		pan.add(lcurrent, c);

		c.gridx = 1;
		current = new JPasswordField();
		current.setPreferredSize(new Dimension(100, 25));
		pan.add(current, c);

		c.gridx = 0;
		c.gridy = 1;
		JLabel lTime = new JLabel(messages.getString("New_pass") + ":");
		pan.add(lTime, c);

		c.gridx = 1;
		newPass = new JPasswordField();
		newPass.setPreferredSize(new Dimension(100, 25));
		pan.add(newPass, c);

		c.gridx = 0;
		c.gridy = 2;
		JLabel lIncre = new JLabel(messages.getString("Confirm_New_pass") + ":");
		pan.add(lIncre, c);

		c.gridx = 1;
		confirm = new JPasswordField();
		confirm.setPreferredSize(new Dimension(100, 25));
		pan.add(confirm, c);

		c.gridx = 0;
		c.gridy = 5;
		ok = new JButton(messages.getString("Ok"));
		ok.addActionListener(this);
		pan.add(ok, c);

		c.gridx = 1;
		c.gridwidth = 2;
		canc = new JButton(messages.getString("Cancel"));
		canc.addActionListener(this);

		pan.add(canc, c);

		this.add(pan);

		this.setLocation(30, 30);
		this.pack();
		this.setPreferredSize(new Dimension(400, 400));
		return this;
	}

	public void actionPerformed(ActionEvent a) {
		if (a.getSource() == ok) {
			String command = "password ";
			command += String.valueOf(current.getPassword()) + " ";
			if (String.valueOf(newPass.getPassword()).equals(
					String.valueOf(confirm.getPassword()))) {
				command += String.valueOf(newPass.getPassword());
				// logger.debug("PASS:" + command);
				prefs.put("password", String.valueOf(newPass.getPassword()));
				Reader.sendString(command);
				this.setVisible(false);
			} else {
				Desktop.infoMessage(messages.getString("Doesnt_match"));
			}
		}
		if (a.getSource() == canc) {
			this.setVisible(false);
		}
	}
}
